<?php

class Admin_ObjectController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	Zend_Layout::startMvc(array("layoutPath"=>APPLICATION_PATH."/layouts/scripts","layout"=>"admin"));
    	$auth = Zend_Auth::getInstance();
    	$infoUser = $auth->getIdentity();
    	$this->view->fullName = $infoUser->ADMINNAME;
    	$this->view->role = $infoUser->ROLE_ID;
    	$this->_model = new Default_Model_DbTable_Object();
    	$this->upload = new Private_Upload();
    }

    public function indexAction()
    {
        // action body
    }

    public function architectAction(){
    	$this->view->headTitle("Quản Lý Kiến Trúc Sư","PREPEND");
    	$this->view->title = "Quản Lý Kiến Trúc Sư";
    	$architects = $this->_model->ShowObjByDetai("Kien Truc Su");
    	$this->view->allArc = $architects;
    	
    	if($this->_request->isPost()){
    		$page_id = 2;
    		$name = $this->_getParam("Name");
    		$title = $this->_getParam("Title");
    		$email = $this->_getParam("Email");
    		$desc = $this->_getParam("Desc");
    		$content = $this->_getParam("Content");
    		$detail = "Kien Truc Su";
    		$classify = "Gian Hang";
    		$stt = $this->_getParam("Stt");
    		if($_FILES["Img"]["name"]=="" and $this->_getParam("Link") == ""){
    			$img = "/homeviet/public/uploaded/images/noavatar.png";
    		}elseif ($this->_getParam("Link")){
    			$img = $this->_getParam("Link");
    		}else{
    			$img = "/homeviet/public/uploaded/images/architect/".$_FILES["Img"]["name"];
    			$this->upload->upload("architect");
    		}
    		$this->_model->addObject($page_id, $img, $name, $title, $desc, $content, $detail, $email, $classify, $stt);
    		$this->_redirect("admin/object/architect");
    	}
    }
    
    public function productAction(){
    	$this->view->headTitle("Quản Lý Sản Phẩm","PREPEND");
    	$this->view->title = "Quản Lý Sản Phẩm";
    	$architects = $this->_model->ShowObjByDetai("Noi Ngoai That");
    	$pages = new Default_Model_DbTable_Page();
    	$this->view->allPage = $pages->getAllPage();
    	$this->view->allPro = $architects;
    	if($this->_request->isPost()){
    		$page_id = $this->_getParam("Parent");
    		$name = $this->_getParam("Name");
    		$title = $this->_getParam("Title");
    		$email = $this->_getParam("Email");
    		$desc = $this->_getParam("Desc");
    		$content = $this->_getParam("Content");
    		$detail = "Noi Ngoai That";
    		$classify = $this->_getParam("Classify");
    		$stt = $this->_getParam("Stt");
    		if($_FILES["Img"]["name"]=="" and $this->_getParam("Link") == ""){
    			$img = "/homeviet/public/uploaded/images/noavatar.png";
    		}elseif ($this->_getParam("Link")){
    			$img = $this->_getParam("Link");
    		}else{
    			$img = "/homeviet/public/uploaded/images/product/".$_FILES["Img"]["name"];
    			$this->upload->upload("product");
    		}
    		$this->_model->addObject($page_id, $img, $name, $title, $desc, $content, $detail, $email, $classify, $stt);
    		$this->_redirect("admin/object/product");
    	}
    }
    
    public function editaAction(){
    	$this->view->headTitle("Chỉnh Sửa Kiến Trúc Sư","PREPEND");
    	$this->view->title = "Chỉnh Sửa Kiến Trúc Sư";
    	$id = $this->_getParam("id");
    	$this->view->Arc = $this->_model->getObjectById($id);
    	if($this->_request->isPost()){
    		$page_id = 2;
    		$name = $this->_getParam("Name");
    		$title = $this->_getParam("Title");
    		$email = $this->_getParam("Email");
    		$desc = $this->_getParam("Desc");
    		$content = $this->_getParam("Content");
    		$detail = Architect;
    		$classify = Store;
    		$stt = $this->_getParam("Stt");
    		if($_FILES["Img"]["name"]=="" and $this->_getParam("Link") == ""){
    			$img = "/homeviet/public/uploaded/images/noavatar.png";
    		}elseif ($this->_getParam("Link")){
    			$img = $this->_getParam("Link");
    		}else{
    			$img = "/homeviet/public/uploaded/images/".$_FILES["Img"]["name"];
    			$this->upload->upload("architect");
    		}
    		$this->_model->updateObject($id, $page_id, $img, $name, $title, $desc, $content, $detail, $email, $classify, $stt);
    		$this->_redirect("admin/object/architect");
    	}
    }
    
    public function editpAction(){
    	$this->view->headTitle("Chỉnh Sửa Sản Phẩm","PREPEND");
    	$this->view->title = "Chỉnh Sửa Sản Phẩm";
    	$pages = new Default_Model_DbTable_Page();
    	$this->view->allPage = $pages->getAllPage();
    	$id = $this->_getParam("id");
    	$this->view->Product = $this->_model->getById($id);
    	if($this->_request->isPost()){
    		$page_id = $this->_getParam("Parent");
    		$name = $this->_getParam("Name");
    		$title = $this->_getParam("Title");
    		$email = null;
    		$desc = $this->_getParam("Desc");
    		$content = $this->_getParam("Content");
    		$detail = Interior;
    		$classify = $this->_getParam("Classify");
    		$stt = $this->_getParam("Stt");
    		if($_FILES["Img"]["name"]=="" and $this->_getParam("Link") == ""){
    			$img = "/homeviet/public/uploaded/images/noavatar.png";
    		}elseif ($this->_getParam("Link")){
    			$img = $this->_getParam("Link");
    		}else{
    			$img = "/homeviet/public/uploaded/images/".$_FILES["Img"]["name"];
    			$this->upload->upload("architect");
    		}
    		$this->_model->updateObject($id, $page_id, $img, $name, $title, $desc, $content, $detail, $email, $classify, $stt);
    		$this->_redirect("admin/object/product");
    	}
    }
    
    public function delAction(){
    	$olp = new Default_Model_DbTable_ObjectLibPhoto();
    	$id = $this->_getParam("id");
    	$opp = new Default_Model_DbTable_ObjectPhotoProfile();
    	
    	$obj = $this->_model->getById($id);
    	
    	$patch = $obj["OBJECT_IMG"];
    	if ($patch != "/homeviet/public/uploaded/images/noavatar.png"){
    		$this->upload->del_img(ROOT_DIR.$patch);
    	}
    	$libphoto = $olp->getObjectLibPhotoByObjectId($id);
    	foreach ($libphoto as $libid){
    		$lid = $libid["ID"];
    		$opp->removeByLibID($lid);
    	}
    	$opp->removeByObjectID($id);
    	$olp->removeByObjectID($id);
    	$this->_model->removeObject($id);
    	$this->_redirect("admin/object/architect");
    }
    
    public function delpAction(){
    	$olp = new Default_Model_DbTable_ObjectLibPhoto();
    	$id = $this->_getParam("id");
    	$opp = new Default_Model_DbTable_ObjectPhotoProfile();
    	
    	$obj = $this->_model->getById($id);
    	
    	$patch = $obj["OBJECT_IMG"];
    	if ($patch != "/homeviet/public/uploaded/images/noavatar.png"){
    		$this->upload->del_img(ROOT_DIR.$patch);
    	}
    	$libphoto = $olp->getObjectLibPhotoByObjectId($id);
    	foreach ($libphoto as $libid){
    		$lid = $libid["ID"];
    		$opp->removeByLibID($lid);
    	}
    	$opp->removeByObjectID($id);
    	$olp->removeByObjectID($id);
    	$this->_model->removeObject($id);
    	$this->_redirect("admin/object/product");
    }
    
    public function activeAction(){
    	$id = $this->_getParam("id");
    	$obj = $this->_model->getById($id);
    	$this->_model->activeObject($id);
    	#die($obj["DETAIL"]);
    	if($obj["DETAIL"]=="Kiến Trúc Sư")
    	{
    		$this->_redirect("admin/object/architect");
    	}else{
    		$this->_redirect("admin/object/product");
    	}
    }

}

